#!/bin/bash

function getStrValue(){
	grepKey=$1
	grepFile=$2
	grep "${grepKey}" $grepFile |head -n 1| awk -F":" '{print $2}' | awk '{gsub(/^\s+|\s+$/, "");print}'

}

## 通过dmidecode 获取服务器信息
function getSystemInfo(){
	systemInfoFile='/tmp/dmidecode.system.log'
	dmidecode -t system >$systemInfoFile
	systemValue=`getStrValue 'Manufacturer' $systemInfoFile`
	echo -n "systemManufacturer=${systemValue};"

	systemValue=`getStrValue 'Product Name' $systemInfoFile`
	echo -n "systemProductName=${systemValue};"

	systemValue=`getStrValue 'Serial Number' $systemInfoFile`
	# echo -n "systemSerialNumber=${systemValue};"
	echo -n "systemSerialNumber=SIA04120001;"
	

}

## 通过dmidecode 获取主板信息
function getBaseBoardInfo(){

	dmidecodeFile='/tmp/dmidecode.baseboard.log'
	dmidecode -t baseboard >$dmidecodeFile

	systemValue=`getStrValue 'Manufacturer' $dmidecodeFile`
	echo -n "baseboardManufacturer=${systemValue};"

	systemValue=`getStrValue 'Product Name' $dmidecodeFile`
	echo -n "baseboardProductName=${systemValue};"

	systemValue=`getStrValue 'Serial Number' $dmidecodeFile`
	echo -n "baseboardSerialNumber=${systemValue};"

	systemValue=`getStrValue 'Version' $dmidecodeFile`
	echo -n "baseboardVersion=${systemValue};"
}


## 通过dmidecode 获取bios信息
function getBiosInfo(){

	dmidecodeFile='/tmp/dmidecode.bios.log'
	dmidecode -t bios >$dmidecodeFile

	systemValue=`getStrValue 'Version:' $dmidecodeFile`
	echo -n "biosVersion=${systemValue};"
}

## 通过dmidecode 获取IPMI信息
function getBMCInfo(){

	bmcFile='/tmp/bmc.mc.log'
	ipmitool mc info >$bmcFile
	if [ $? -ne 0 ];then
		## 不支持ipmitool
		return
	fi

	## 获取bmc 版本
	systemValue=`getStrValue 'Firmware Revision' $bmcFile`
	echo -n "bmcFirmwareVersion=${systemValue};"

	ipmiIP=`ipmitool lan print  |grep 'IP Address' |grep -v 'IP Address Source' |awk -F':' '{print $2}'`
	echo -n "bmcIpAddress=${ipmiIP}"
}

function main(){
	getSystemInfo
	getBaseBoardInfo
	getBiosInfo
	getBMCInfo


}
main